import { createRouter, createWebHistory } from 'vue-router'
import loginIndex from '@/views/login/index.vue'
import { useUserInfoStore } from '@/stores/modules/userInfo';
import {  ROUTER_WHITE_LIST } from '@/config' 

declare module 'vue-router' {
  interface RouteMeta {
    icon?: string,
    hidden?: boolean,
    title: string,
    isFront?: boolean,
  }
 }

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: loginIndex
    },
    {
      path: '/applyTest',
      name: 'applyTest',
      component: () => import('@/views/login/applyTest.vue')
    },
    {
      path: '/test',
      name: 'test',
      component: () => import('@/views/test.vue')
    },
    {
      path: '/findPassWord',
      name: 'findPassWord',
      component: () => import('@/views/login/findPassWord.vue')
    },
    {
      path: "/shareMessage/:id", //分享页面
      name: "shareMessage",
      components: {
        default: () => import('@/views/shareMessage/index.vue'),
        sider: () => import('@/views/ChatSider.vue')
      }
    },
    {
      path: "/unloginShareMessage/:id", //未登录分享页面
      name: "unloginShareMessage",
      component: () => import("@/views/shareMessage/unLogin.vue")
    },
    {
      path: "/invite/:id",
      name: "invite",
      component: () => import("@/views/login/invite.vue")
    },
    {
      path: "/inviteMobile/:id",
      name: "InviteMobile",
      component: () => import("@/views/login/InviteMobile.vue")
    },
    {
      path: '/login/:id',
      name: 'login',
      component: loginIndex
    },
    {
      path: '/adminLogin',
      name: 'adminLogin',
      component: () => import('@/views/AminLogin.vue')
    },
    {
      path: '/chat',
      name: 'chat',
      component: () => import('@/components/ChatLayout.vue'),
      children: [
        {
          path: 'chatDefault',
          name: 'chatDefault',
          meta:{
            icon: 'IconChatHistory',
            hidden: true,
            title: '新建对话',
            isFront: true
          },
          components: {
            default: () => import('@/components/ChatRoom.vue'),
            sider: () => import('@/views/ChatSider.vue')
          }
        },
        {
          path: 'personalCenter',
          name: 'PersonalCenter',
          components: {
            default: () => import('@/views/PersonalCenter.vue'),
            sider: () => import('@/views/ChatSider.vue')
          },
          meta: {
            title: '基础信息',
            isFront: false,
            hidden: false,
            icon: 'icon_basic'
          }
        },
        {
          path: '',
          name: 'UserTeamManage',
          redirect: '/chat/memberManage',
          meta: {
            title: '团队管理',
            isFront: false,
            hidden: false,
            icon: 'icon_team',
            isAdmin: true // 管理者
          },
          children: [
            {
              path: 'memberManage',
              name: 'MemberManage',
              components: {
                default: () => import('@/views/MemberManage.vue'),
                sider: () => import('@/views/ChatSider.vue')
              },
              meta: {
                title: '成员管理',
                isFront: false,
                hidden: false,
                icon: 'icon_member'
              }
            },
            {
              path: 'ownerTeamSetting',
              name: 'OwnerTeamSetting',
              components: {
                default: () => import('@/views/OwnerTeamSetting.vue'),
                sider: () => import('@/views/ChatSider.vue')
              },
              meta: {
                title: '团队设置',
                isFront: false,
                hidden: false,
                icon: 'icon_setting',
                isOwner: true
              }
            }
          ]
        },
        {
          path: 'documentIndex',
          name: 'documentIndex',
          meta:{
            icon: 'IconDocument',
            hidden: false,
            title: '解读文档',
            isFront: true
          },
          components: {
            default: () => import('@/views/document/documentIndex.vue'),
            sider: () => import('@/views/ChatSider.vue')
          }
        },
        {
          path: 'documentParsing/:id?',
          name: 'documentParsing',
          meta:{
            icon: 'IconDocument',
            hidden: true,
            title: '解读文档',
            isFront: true
          },
          components: {
            default: () => import('@/views/document/DocumentParsing.vue'),
            sider: () => import('@/views/ChatSider.vue')
          }
        },
        {
          path: 'DocumentUpload',
          name: 'DocumentUpload',
          meta:{
            icon: 'IconDocument',
            hidden: true,
            title: '文件上传',
            isFront: true
          },
          components: {
            default: () => import('@/views/document/DocumentUpload.vue'),
            sider: () => import('@/views/ChatSider.vue')
          }
        },
        {
          path: 'chatList',
          name: 'chatList',
          meta:{
            icon: 'IconChatHistory',
            hidden: false,
            title: '历史记录',
            isFront: true
          },
          components: {
            default: () => import('@/views/ChatList.vue'),
            sider: () => import('@/views/ChatSider.vue')
          }
        },
        {
          path: ':id/:pdfFile?',
          name: 'chatDetail',
          meta:{
            icon: 'IconChatHistory',
            hidden: true,
            title: '对话详情',
            isFront: true
          },
          components: {
            default: () => import('@/components/ChatRoom.vue'),
            sider: () => import('@/views/ChatSider.vue')
          }
        },
        {
          path: "loginShareMessage/:id", //登录分享页面
          name: "loginShareMessage",
          meta:{
            icon: 'IconChatHistory',
            hidden: true,
            title: '分享',
            isFront: true
          },
          components: {
            default: () => import('@/components/ChatRoom.vue'),
            sider: () => import('@/views/ChatSider.vue')
          }
        },
      ]
    },
    {
      path: '/paper',
      name: 'paper',
      component: () => import('@/components/PaperLayout.vue'),
      children: [
        {
          path: ':id',
          components: {
            default: () => import('@/components/PaperView.vue'),
            sider: () => import('@/views/PaperSider.vue')
          }
        }
      ]
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('@/views/AboutView.vue')
    },
    {
      path: '/manage',
      name: 'manage',
      component: () => import('@/components/MainLayout.vue'),
      children: [
        {
          path: 'auditManage',
          name: 'AuditManage',
          components: {
            default: () => import('@/views/AuditManage.vue'),
            sider: () => import('@/components/AdminSider.vue')
          },
          meta: {
            title: '审核管理',
            hidden: false
          }
        },
        {
          path: 'teamManage',
          name: 'TeamManage',
          components: {
            default: () => import('@/views/TeamManage.vue'),
            sider: () => import('@/components/AdminSider.vue')
          },
          meta: {
            title: '团队管理',
            hidden: false
          }
        },
        {
          path: 'dialogSetting',
          name: 'DialogSetting',
          components: {
            default: () => import('@/views/DialogSetting.vue'),
            sider: () => import('@/components/AdminSider.vue')
          },
          meta: {
            title: '对话设置',
            hidden: false
          }
        },
        {
          path: 'user',
          name: 'UserManage',
          components: {
            default: () => import('@/views/UserManage.vue'),
            sider: () => import('@/components/AdminSider.vue')
          },
          meta: {
            title: '账户管理'
          }
        },
      ]
    }
  ]
})



// 路由前置守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserInfoStore()
  const isAdminRouter = to.path.includes('/manage')
  console.log('to.path',to.path,isAdminRouter)
  if(isAdminRouter && !userStore.token) {
    next({ path: '/adminLogin', replace: true ,force:true })
  } else if((to.path.includes('/adminLogin') || isAdminRouter) && userStore.token){
    next()
  } else if (to.path !== "/" && !userStore.frontToken && !ROUTER_WHITE_LIST.includes(to.name as string)) {
    // 即使位置与当前位置相同，也会触发导航。
    // 注意，这也将向历史记录中添加一个新条目，除非“replace:true”`通过
    // 添加 force 字段后，地址栏不会在登录失效后，跳转到其他页面，点击history只会停留在/login
    next({ path: "/", replace: true ,force:true });
  } else if(to.path === '/' && userStore.frontToken) {
    next({ name: 'chatDefault', replace: true ,force:true  })
  } else {
    next();
  }
});
export default router
